package com.stnet.dao.enterprice.chinamobile;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.Directorder;

/**
 * @author Messcat
 * @version 1.1
 * 
 */
public class DirectorderDaoImpl extends BaseDaoImpl implements DirectorderDao {

	public DirectorderDaoImpl() {
	}

	public void delete(Directorder directorder) {
		getHibernateTemplate().delete(directorder);
	}

	public void delete(Long id) {
		getHibernateTemplate().delete(get(id));
	}

	public List findAll() {
		List find = getHibernateTemplate().find("from Directorder");
		return find;
	}

	public void save(Directorder directorder) {
		getHibernateTemplate().save(directorder);
	}

	public void update(Directorder directorder) {
		getHibernateTemplate().merge(directorder);
	}

	public Directorder get(Long id) {
		return (Directorder) getHibernateTemplate().get(Directorder.class, id);
	}

	public List findDirectByAgent(Long agent) {

		List find = getHibernateTemplate().find(
				"from Directorder where user.id = ? order by id desc", agent);
		return find;

	}

	public Pager getListByAgent(int pageSize, int pageNo, Long agent) {
		Session session = null;
		Pager pager = null;

		session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Directorder.class);

		criteria.add(Restrictions.eq("user.id", agent));
		criteria.addOrder(Order.desc("editetime"));
		int rowCount = ((Integer) criteria
				.setProjection(Projections.rowCount()).uniqueResult())
				.intValue();
		criteria.setProjection(null);
		int startIndex = pageSize * (pageNo - 1);
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List result = criteria.list();
		pager = new Pager(pageSize, pageNo, rowCount, result);

		session.close();

		return pager;
	}
}
